// Main.java
package cc.magicjson.example.demo.monitor.mbean;

import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory;
import java.util.stream.Collectors;

import static java.util.Arrays.stream;

/**
 * 测试简单的计数MBean
 */
public class Main {
    public static void main(String[] args) throws Exception {

        stream(of(stream(SimpleCounter.class.getDeclaredFields())
                .map(s -> s.toString().substring(s.toString().lastIndexOf(".") + 1))
                .collect(Collectors.toList())))
                .forEach(s -> System.out.println(s));

        // 获取平台MBean服务器
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

        // 创建MBean实例
        SimpleCounter counter = new SimpleCounter();


        String packageName = SimpleCounter.class.getPackage().getName();

        String classSimpleName = SimpleCounter.class.getSimpleName();
        // 创建MBean对象名
        ObjectName name = new ObjectName(packageName + ":type=" + classSimpleName);

        // 将MBean注册到MBean服务器
        mbs.registerMBean(counter, name);

        System.out.println("SimpleCounter MBean has been registered in the MBean server.");
        // 在此处，您可以使用JMX客户端连接到本地MBean服务器并管理这个MBean

        System.out.println("press key to exit");

        System.in.read();


    }

    private static <T> T[] of(T... values) {
        return values;
    }


}